package zhtt.example.controller;

import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.ResponseEntity;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import zhtt.example.fegin.MicroservicesApplicationFeginClient;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhaoq on   2024/7/23 16:36
 **/
@RestController
public class GatewayController {

    private final String serviceName="microservices-nacos";

    @Autowired
    private DiscoveryClient discoveryClient;

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private WebClient webClient;

    @Autowired
    private  WebClient.Builder webClientBuilder;

    @Lazy
    @Resource
    private MicroservicesApplicationFeginClient microservicesApplicationFeginClient;

    @RequestMapping("user")
    private String password() {
        return "user";
    }

    /**
     * 通过IP的形式
     * @return
     */
    @RequestMapping("rest0")
    private Mono<String> rest0() {
        String url = "http://localhost:8040/code/aaa";
        Mono<String> result = webClient.get()
                .uri(url)
                .retrieve()
                .bodyToMono(String.class);
        return result;
    }

    /*@RequestMapping("rest1")
    private Mono<ResponseEntity<String>> rest1() {
        ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8040/code/aaa", String.class);
        Mono<ResponseEntity<String>> result = Mono.just(response);
        return result;
    }*/

    /**
     * 通过服务名的形式
     * @return
     */
    @RequestMapping("rest1")
    private Mono<String> rest1() {
        return webClientBuilder.build()
                .get()
                .uri("http://"+serviceName+"/code/{id}", "code")
                .retrieve()
                .bodyToMono(String.class);
    }

    @RequestMapping("rest2")
    private Mono<String> rest2() {
        String endpoint = "code/aaa";
        ServiceInstance serviceInstance = loadBalancerClient.choose(serviceName);
        if (serviceInstance != null) {
            String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/" + endpoint;
            Mono<String> result = webClient.get()
                    .uri(url)
                    .retrieve()
                    .bodyToMono(String.class);
            return result;
        } else {
            Mono<String> result = Mono.just("服务暂时不可用!");
            return result;
        }
    }



    @RequestMapping("fegin")
    private Mono<String> code() {
        Mono<String> mono = microservicesApplicationFeginClient.getUserByCode("aaaa");
        return mono;
    }
}
